package cn.devices.mapper;


import cn.devices.dto.AndroidUserDto;
import cn.devices.dto.DmUserDto;
import cn.devices.entity.TbUser;
import cn.devices.model.UserModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 员工表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbUserMapper {

    @Insert("insert into tb_user(user_id, dep_code, user_name, user_photo, user_pos, user_status, disabled,telephone)\n" +
            " VALUE (#{userId},#{depCode},#{userName},#{userPhoto},#{userPos},#{userStatus},0,#{telephone})")
    int tbUserAdd(TbUser user);

    @Update("update tb_user set dep_code=#{depCode},user_name=#{userName}," +
            "user_pos=#{userPos},user_status=#{userStatus},telephone=#{telephone} where user_id=#{userId}")
    int tbUserUpdate(TbUser user);

    @Update("update tb_user set disabled=1 where user_id=#{userId}")
    int tbUserDeleteFalse(String userId);

    @Update("delete from tb_user where user_id=#{userId}")
    int tbUserDeleteTrue(String userId);

    @Select("select count(*) from tb_user where telephone=#{telephone} and user_id!=#{userId}")
    int countTel(String telephone, String userId);

    @Select("select * from  tb_user  left join tb_dm td on td.dep_code = tb_user.dep_code where user_id=#{userId}")
    TbUser userGetById(String userId);


    @Select("<script> " +
            "select *\n" +
            "from tb_user\n" +
            "         left join tb_dm td on td.dep_code = tb_user.dep_code\n" +
            "where \n" +
            "  td.disabled = 0\n" +
            "  and tb_user.disabled = 0\n" +
            "<if test='depName != null and depName!=\"\"'>" +
            " and dep_name = #{depName}\n" +
            "</if>" +
            "" +
            " </script>")
    List<TbUser> tbUserList(String depName);


    @Select("select * from tb_user where dep_code=#{depCode}")
    List<TbUser> dpUserList(String depCode);

    @Results(id = "usMap",
            value = {
                    @Result(column = "userList", property = "userList", typeHandler = cn.devices.typehandler.JsonTypeHandler.class),
                       })
    @Select("select td.*,\n" +
            "       concat('[',  GROUP_CONCAT(\n" +
            "               CONCAT('{\"user_id\":\"', tu.user_id, '\"'), ',',\n" +
            "               concat('\"user_name\":\"', tu.user_name, '\"'), '}')\n" +
            "           , ']') as userList\n" +
            "from tb_dm td\n" +
            " left join (select * from  tb_user where disabled =0) tu on td.dep_code = tu.dep_code\n" +
            "         where td.disabled = 0 group by td.dep_code")
    List<DmUserDto> dmUserDtoList();


    @Select("select * from tb_dm where disabled=0")
    List<DmUserDto> dmUserDtos();

    @Select("select * from tb_user where disabled=0")
    List<UserModel> userModelList();

    @Select("select * from tb_user tu left join tb_dm td on tu.dep_code = td.dep_code\n" +
            "where tu.disabled=0")
    List<AndroidUserDto> androidUserDtos();
}
